package com.earnings.okhttputils.utils.ui.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.earnings.R;
import com.earnings.okhttputils.utils.UrlConstant.Constant;
import com.earnings.okhttputils.utils.adapter.DataBindingBaseadapter;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.god.Listener.GodOnClickListener;
import com.earnings.okhttputils.utils.ui.BaseFragment;
import com.earnings.okhttputils.utils.ui.activity.common.RecordActivity;
import com.earnings.okhttputils.utils.ui.fragment.transaction.RechargeFragment;
import com.earnings.okhttputils.utils.utils.PagerSlidingTabStripUtils;
import com.earnings.okhttputils.utils.view.PagerSlidingTabStrip;

import java.util.ArrayList;


/*
充值页面-话费、油卡
 */
public class RechargeActivity extends GodLeftHandBaseActivity implements PagerSlidingTabStrip.OnPagerTitleItemClickListener, GodOnClickListener {
    private RecyclerView recyclerView;
    private DataBindingBaseadapter buttonAdapter;
    private PagerSlidingTabStrip tabs;
    private ViewPager viewpager;
    public static final int RECHARGE_PHONE = 1;
    public static final int RECHARGE_OIL = 2;
    private int type;


    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_recharge;
    }

    @Override
    protected void init() {
        setTitle("话费充值");
        showRightTextView("预缴订单");

        tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        setOnClickListener( this,R.id.top_right_tv );
        initTab();
    }


    public void initTab() {

        ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
        if (bundleData.containsKey("type")) type = bundleData.getInt("type");
        switch (type) {
            case RECHARGE_PHONE:
                super.setTitle("手机充值");
                RechargeFragment atFragment = new RechargeFragment();
                RechargeFragment btFragment = new RechargeFragment();
                fragmentList.add(atFragment);
                fragmentList.add(btFragment);
                atFragment.Type(RechargeFragment.RECHARGE_BILL);
                btFragment.Type(RechargeFragment.RECHARGE_FLOW);
                atFragment.setTitle("话费充值");
                btFragment.setTitle("流量充值");
                break;
            case RECHARGE_OIL:
                super.setTitle("油卡充值");
                RechargeFragment ctFragment = new RechargeFragment();
                RechargeFragment dtFragment = new RechargeFragment();
                fragmentList.add(ctFragment);
                fragmentList.add(dtFragment);

                ctFragment.Type(RechargeFragment.RECHARGE_SINOPEC);
                dtFragment.Type(RechargeFragment.RECHARGE_PETROLEUM);
                ctFragment.setTitle("中国石化");
                dtFragment.setTitle("中国石油");
                break;
        }


        viewpager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));

        tabs.setViewPager(viewpager);
        PagerSlidingTabStripUtils.setDefault(tabs, this);

        tabs.setOnPagerTitleItemClickListener(this);
    }

//    @Override
//    public void onClick(View v) {
//
//        int i = v.getId();
//        if (i == R.id.top_right_tv) {
//            bundleData.putString("title", "转账记录");
//            //   skipActivity( RecordActivity.class );
//
//        } /*else if (i == R.id.turnfriend) {
//        } else if (i == R.id.turnuser) {
//         //   skipActivity( TurnUserActivity.class );
//
//        }*/
//    }

    @Override
    public void onSingleClickItem(int position) {
        viewpager.setCurrentItem(position);
    }

    @Override
    public void onDoubleClickItem(int position) {

    }

    @Override
    public void OnClickListener(View view) {
        if(view.getId()==R.id.top_right_tv){
            if(type==RECHARGE_PHONE)
            bundleData.putInt( "type" , Constant.RECORD_TYPE_RECHARGE_PHONE);
            else if(type==RECHARGE_OIL)
                bundleData.putInt( "type" , Constant.RECORD_TYPE_RECHARGE_OIL);
            skipActivity( RecordActivity.class );
        }
    }

    class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        ArrayList<Fragment> list;

        public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
            super(fm);
            this.list = list;

        }


        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Fragment getItem(int arg0) {
            return list.get(arg0);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            BaseFragment fragment = (BaseFragment) getItem(position);
            return fragment.getTitle();
        }
    }
}
